package com.app.util.encrypt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;



/**
 * get cpu, disk info
 *
 * @author LiMengyan
 *
 */
@SuppressWarnings("unused")
public class HardWareInfoGetter {
	private final String LINUX_NAME = "linux";
	private final String SOLARIS_NAME = "solaris";
	private final String HPUNIX_NAME = "hpunix";
	private final String AIX_NAME = "aix";

	private Runtime rt = Runtime.getRuntime();

	public HardWareInfoGetter() {

	}

	private BufferedReader getProcesserReader(String cmd) throws IOException {
		Process process = rt.exec(cmd);

		InputStream in = null;
		InputStreamReader ir = null;
		BufferedReader br = null;

		in = process.getInputStream();
		ir = new InputStreamReader(in);
		br = new BufferedReader(ir);

		return br;
	}

	private String whatOS() throws IOException {
		String result = "";
		BufferedReader br = null;
		try {
			br = getProcesserReader("uname");
			String line = "";

			while ((line = br.readLine()) != null) {
				if (Pattern.matches(".*[lL]unix.*", line)) {
					result = LINUX_NAME;
					break;
				} else if (Pattern.matches(".*[sS]un.*", line)) {
					result = SOLARIS_NAME;
					break;
				} else {
					result = "";
					break;
				}
			}
		} finally {
			if (br != null)
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		return result;
	}

	private String getHostIdInfo() throws Exception {
		String result = "";
		String line = "";
		BufferedReader br = null;

		br = getProcesserReader("hostid");
		while ((line = br.readLine()) != null) {
			if (line != "") {
				result = line;
				break;
			}
		}
		return result;
	}

	private String getHardDiskInfo() {
		return null;
	}

	private String getCpuInfo() {
		return null;
	}

	private String getMacAddressInfo() {
		String mac = GetMacAddress.getMACAddress();
		return mac;
	}

	public String getEncryHardWareInfo() {
		String value = null;
		try {
			whatOS();
			value = getHostIdInfo() + "," + getMacAddressInfo() + ","
					+ getHardDiskInfo();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			value = "a4h3gh8";
		}
		return DesCrypt.encrypt(value);
	}

	public static void main(String[] args) {
		HardWareInfoGetter hwig = new HardWareInfoGetter();

		System.out.println(Pattern.matches(".*[lL]unix.*", "jdflkasjf;"));

//		System.out.println(hwig.getHostIdInfo());
	}
}
